<?php
namespace app\api\controller;
/**
 *update
 */
use think\Controller;
use think\Db;
use think\Cache;
use app\api\model\ApiAuthorizationModel;
use app\admin\model\AdminMenuModel;
use app\admin\model\AccessModel;
use app\admin\model\ModuleMenuModel;
use app\api\model\ApiAccessAuthorityModel;
use app\api\controller\ApiAccessAuthorityController;
use plugins\appmarket\model\AdminIndexModel as AppmarketAdminIndexModel;
use plugins\appmarket\controller\ConfigController as AppmarketConfigController; 
use plugins\appmarket\model\ModulePurchaseHistoryModel; 

use app\api\controller\ApiAccessEncodeController;


/**
 * 
 */
class ApiUpdateController extends Controller
{

	
	
	public function  _initialize()
    {
        
    }
	
	//上传更新数据
	public function updateRecord(){
		//if( !$this->request->isPost()){ return zy_json_echo (false, '请求类型错误!'); }
		$packagePath = ROOT_PATH."public/upload/systemUpdate";
		$param = $this->request->param();
		
		/*
		
		$streamData = isset($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
 
		if(empty($streamData)){
			$streamData = file_get_contents('php://input');
		}
		if( $streamData == "" ){
			return zy_json_echo(false, '未获取到数据！');
		}
		$savePath = $packagePath.DS.$param['name'];
		try {
			if( !file_exists( $packagePath ) ){
				mkdir( $packagePath , 0777 );
			}
            file_put_contents($savePath, $streamData, true);
	   } catch ( \Exception $e) {
		   return zy_json_echo(false , '异常：'.$e->getMessage());
	   }
	   */
		$data = [
			'name' => $param['name'],
			'status' => 1,
			'add_time' => time(),
			'version' => $param['version'],
			'type' => $param['type'],
			'source' => "upload/systemUpdate/".$param['name']
		];
		$res = DB::name( 'system_update_record' )->insertGetId( $data );
		if( !$res ){
			return zy_json_echo( false , "数据保存失败 ！" );
		}
		return zy_json_echo(true , "数据上传成功！" , $res );
	}
	
	public function updateDetail()
	{
		if( !$this->request->isPost() ){
			return zy_json_echo(false , '请求类型错误!');
		}
		$data = $this->request->param();
		$detail = json_encode ( $data[ 'detail' ] ,  JSON_UNESCAPED_UNICODE ); 
		$id = $data['id'];
		$res = Db::name( 'system_update_record' )->where( 'id' , $id )->update( [ 'detail' => $detail ] );
		if( !$res ){
			return zy_json_echo( false , '更新失败！' );
		}
		return zy_json_echo( true , '更新成功!' );
	}
	
	
	public function topNews()
	{
		if( !$this->request->isPost() ){
			return zy_json_echo( false , '请求类型错误！' );
		}
		$param = $this->request->param();
		$type =   $param[ 'type' ];
		$info = Db::name('system_update_record')->where('type', $type )->order('add_time desc')->select()->toArray();
		if( empty($info) ){
			return zy_json_echo(false , '未找到更新包！' );
		}
		return zy_json_echo( true , 'is find' ,$info[0]);
	}
	
	public function getVersionDownloadAddr(){
		return $this->topNews();
	}
	
}